package com.baizhi.service.impl;

import com.baizhi.dao.UserDAO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.util.ThreadPollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;
    @Override
    public PageInfo<User> selectAllUser(Integer gid, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<User> users = userDAO.selectAllUser(gid);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public Boolean insertUser(User user) {
        Boolean a = null;
        try{
            userDAO.insertUser(user);
            a = true;
        }catch(Exception e){
            a = false;
        }
        return a;
    }

    @Override
    public Boolean updateUser(User user) {
        Boolean a = null;
        try{
            userDAO.updateUser(user);
            a = true;
        }catch(Exception e){
            a = false;
        }
        return a;
    }

    @Override
    public User selectOneUser(Integer id) {
        User user = userDAO.selectOneUser(id);
        return user;
    }

    @Override
    public List<User> selectCount() {
        List<User> users = userDAO.selectCount();
        return users;
    }

    @Override
    public List<Map> selectWeek(Integer amount) {
        List<Map> maps = userDAO.selectWeek(amount);
        return maps;
    }

    @Override
    public List<Map> selectChina() {
        List<Map> maps = userDAO.selectChina();
        return maps;
    }

    @Override
    public Map selectCommon() throws InterruptedException {

        System.out.println("1231546 = " + 1231546);
//        1.创建三个子线程  初始化计数器
//        创建一个map对象
        Map map = new HashMap<>();
        CountDownLatch countDownLatch = new CountDownLatch(3);
//        让线程池里的随机一个线程调用这个任务
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
//              List<User> users = selectCount();
                map.put("count",selectCount());
//                调用countDown方法
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("a1",selectWeek(1));
                map.put("a2",selectWeek(2));
                map.put("a3",selectWeek(3));
//                调用countDown方法
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
//              调用查询所有省份的方法
                map.put("china",selectChina());
//                调用countDown方法
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        return map;
    }
}
